//! @author Adam Emil Skoog
//! @date   2012-01-05

#ifndef MEHDI_RESOURCE_MANAGER_H_
	#define MEHDI_RESOURCE_MANAGER_H_
	#include <string>
	#include <vector>

	namespace mehdi
	 {
		template<class T> class ResourceManager
		 {
			public:
				void add(const std::string &,const std::string &);
				T *const get(const std::string &) const;
				void loadAll();
				virtual ~ResourceManager() {}

			protected:
				virtual T *const load(const std::string &) const = 0;

			private:
				struct Resource
				 {
					explicit Resource(const std::string &,const std::string &);

					std::string  path,
								 key;
					T           *resource;
				 };

				std::vector<Resource> resources;
		 };

		template<class T> void ResourceManager<T>::add(const std::string &path,const std::string &key)
		 {
			resources.push_back(Resource(path,key));
		 }

		template<class T> T *const ResourceManager<T>::get(const std::string &key) const
		 {
			for (unsigned i(0); i < resources.size(); ++ i)
				if (resources[i].key == key)
					return resources[i].resource;

			return NULL;
		 }

		template<class T> void ResourceManager<T>::loadAll()
		 {
			for (unsigned i(0); i < resources.size(); ++ i)
				resources[i].resource = load(resources[i].path);
		 }

		template<class T> ResourceManager<T>::Resource::Resource(const std::string &path,const std::string &key)
		:
		path    (path),
		key     (key),
		resource(NULL)
		 {
		 }
	 }
#endif
